package com.minivision.fdiot.redis;

import java.nio.ByteBuffer;
import java.util.UUID;

public class RedisIdGenerator {
  
  public static final String nextId(){
    return UUID.randomUUID().toString();
  }

  /**
   * 字节数组转化为UUID
   * @param bytes
   * @return
   */
  public static UUID asUuid(byte[] bytes) {
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    long firstLong = bb.getLong();
    long secondLong = bb.getLong();
    return new UUID(firstLong, secondLong);
  }

  /**
   * UUID转化为字节数组
   * @param uuid
   * @return
   */
  public static byte[] asBytes(UUID uuid) {
    ByteBuffer bb = ByteBuffer.allocate(16);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    return bb.array();
  }
  
}
